// 引入 iscroll
import IScroll from 'iscroll';


// 正则验证字典
const $regExp = {
	$phone: /^1[3456789]\d{9}$/,
	$email: /^([A-Za-z0-9_\-.])+@([A-Za-z0-9_\-.])+\.([A-Za-z]{2,4})$/,
	$pass: /^[A-Za-z0-9]{8,16}$/,
	$realName: /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/,
	$ID: /^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|31)|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}([0-9]|x|X)$/
}
/* 浅拷贝 */
const $copy = Object.assign;
/* 
  //严格类型判断
  $toClass(key:any);
*/
function $toClass(key) {
  return Object.prototype.toString.call(key).split(" ")[1].replace("]", "");
}
/* 
  //类型判断函数 
  $isType(target:any,type:any);
*/
function $isType(target, type) {
  let targetType = $toClass(target);
  return targetType === type;
}
/* 
  // 判断对象是否具有某个属性
  $hasOwn(obj:Object,key:String);
*/
function $hasOwn(obj,key){
  if( obj && $isType(obj,"Object") ){
    return Object.hasOwnProperty.call(obj,key);
  }else{
    throw new Error(`the obj params classType is not a Object`);
  }
}
/* 
  //字符串除去空格 
  $trimAll(str:String);
*/
function $trimAll(str) {
  if (str) {
      return str.replace(/\s/gm, "");
  } else {
      return str;
  }
}


/* 
    // 初始化 iscroll对象
    $initScroll(selector:String|DOMObeject[,ass:Object]);
    <div id="scrollNode">
        <ul>
            <li></li>
            ...
        </ul>
    </div>
    
    // $initScroll("#scrollNode",{ 配置项 });

    //let scrollNode = $("#scrollNode");
    // $initScroll( scrollNode[0] )

    函数返回一个或多个 iScroll实例

*/

function $initScroll(selector, ass) {
    if ('function' == typeof IScroll) {
        if (selector) {
            let scrollEle = typeof selector == "object" ? selector : document.querySelectorAll(selector);
            let lenAuto = typeof scrollEle.length == 'number';
            let eleAuto = lenAuto ? lenAuto : scrollEle;
            if (eleAuto) {
                let defaultOpts = {
                    scrollX: true,
                    scrollY: false,
                    disablePointer: false,
                    click: false,
                    tap: false
                };
                if (ass && $toClass(ass) == "Object") {
                    $copy(defaultOpts, ass);
                }
                let scrollClass;
                if (lenAuto) {
                    let scrollArr = Array.prototype.map.call(scrollEle, function (item) {
                        return new IScroll(item, defaultOpts);
                    });
                    scrollClass = scrollArr.length === 1 ? scrollArr[0] : scrollArr;
                } else {
                    scrollClass = new IScroll(scrollEle, defaultOpts);
                }
                return scrollClass;
            } else {
                console.warn(`the selector Element is not found`);
            }
        } else {
            throw new ReferenceError("the selector is must be required");
        }
    } else {
        throw new ReferenceError('the IScroll is undefined');
    }
}

/* 
    上传图片
    $uploadImg(FileObject);
    主动调用
    <input type="file" accept="image/*" id="fileNode" />

    $("#fileNode").change(function(event){
        // 单张图片
        let baseSrc = uploadImg( event.target.files[0] );
        console.log( baseSrc );

        // 多选图片
        // Array.prototype.forEach.call( event.target.files,function(res)=>{
        //     let baseSrc = uploadImg( res );
        //     console.log( baseSrc );
        // });

        // es6 Array.from( event.target.files ).forEach ...;
    });
*/
function $uploadImg(file) {
	return new Promise((resolve, reject) => {
		let ready = new FileReader();
		ready.readAsDataURL(file);
		try {
			ready.onloadend = (res) => {
				resolve(res.target.result)
			}
		} catch (err) {
			reject(err);
		}
	});
}


/*
	复制文本到剪切板
*/

function $clip( copyText , callback ){
	const doc = document;
	const body = doc.body;
	const insertInput = doc.createElement("input");
	insertInput.setAttribute("style","posttion:fixed;left:-100%;");
	if( copyText && typeof copyText === "string" ){
		insertInput.value = copyText;
	}else{
		throw Error("the params copyText type must be vaild String");
	}
	
	body.appendChild( insertInput );
	try{
		insertInput.select();
		doc.execCommand("copy");
		if( callback && typeof callback === "function" ){
			callback();
		}
		body.removeChild( insertInput );
	}catch(err){
		
		throw Error( err );
	}
}


export {
	$copy,
	$toClass,
	$isType,
	$hasOwn,
	$trimAll,
	$regExp,
	$initScroll,
	$uploadImg,
	$clip
}